<?php
namespace Home\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class CategoryController extends Controller {
    public function _initialize() {
        $user_id = $_SESSION['user_id'];
        $cart_number = M('cart')->where(array('user_id'=>$user_id))->sum('goods_number');
        $this->assign('cart_number',$cart_number);
        $this->assign('act','cate');
    }
    public function index(){
        $cate_mod = M('goods_cat');
        $cate_list = $cate_mod->where(array('cat_id'=>0))->order('sort_order desc,id asc')->select();
        $this->assign('cate_list',$cate_list);
        $this->display();
    }
    public function goods_list(){
        $cat_id = I('cat_id')?I('cat_id'):'';
        $type_id = I('type_id')?I('type_id'):1;
        $mod = M('goods');
        $cate_mod = M('goods_cat');
        $now_category = $cate_mod->where(array('id'=>$cat_id))->find();
        $this->assign('now_category',$now_category);
        $one_cate = $cate_mod->where(array('cat_id'=>0))->order('sort_order desc,id asc')->select();
        $this->assign('one_cate',$one_cate);
        $two_cate = $cate_mod->where(array('cat_id'=>$cat_id))->order('sort_order desc,id asc')->select();
        $sid = I('sid')?I('sid'):$two_cate[0]['id'];
        $this->assign('cat_id',$cat_id);
        $this->assign('sid',$sid);
        $this->assign('type_id',$type_id);
        $this->assign('two_cate',$two_cate);
        $desc = I('desc')?I('desc'):'';
        $this->assign('desc',$desc);
        $this->display();
    }
    public function ajaxGoodsList(){
        $mod = M('goods_store_spec');
        $sid = I('sid')?I('sid'):'';
        $type_id = I('type_id')?I('type_id'):'';
        $desc = I('desc')?I('desc'):'';
        if($sid) {
            if($type_id==1){
                $order_by = 'a.id asc';
            }else if($type_id==2){
                $order_by = 'a.up_time asc';
            }else if($type_id==3){
                $order_by = 'a.sale_number desc';
            }else if($type_id==4){
                $order_by = 'a.spec_price '.$desc;
            }
            $where = array('a.status'=>1,'b.second_cat_id'=>$sid,'b.status'=>1);
            $count = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->where($where)
                ->count();
            $page_count = 10;
            $now_page = I('page') ? I('page') : 1;
            $Page = new AjaxPage($count, $page_count);
            $total_pages = ceil($count / $page_count);
            $is_page = 0;
            if ($now_page >= $total_pages) {
                $is_page = 1;
            }
            $goods_list = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->join('left join ' . C('DB_PREFIX') . 'business as c on c.id=a.store_id')
                ->where($where)
                ->field('a.up_time,b.id as goods_id,b.goods_name,b.thumb,a.store_id,c.store_name,a.id as spec_id,a.spec_price as shop_price,a.spec_name,a.is_sold')
                ->order($order_by)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            foreach ((array)$goods_list as $k => $v) {
                $goods_list[$k]['spec_name'] = $v['spec_name'] ? '规格：' . $v['spec_name'] : '';
                $goods_list[$k]['left_price'] = intval($goods_list[$k]['shop_price']);
                $goods_list[$k]['right_price'] = substr($goods_list[$k]['shop_price'], -3, 3);
            }
            exit(json_encode(array('is_page' =>$is_page, 'result' => $goods_list)));
        }
    }
}